package de.tu_darmstadt.gdi1.battleship.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;

import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidLevelFileException;

public class BattleshipGame {
	
	// The map
	private ExtendedField[][] humanMap = null;
	private ExtendedField[][] cpuMap = null;

	// Whether it is players (true) or cpus (false) turn
	public static boolean PLAYERS_TURN = true;
	public static boolean CPUS_TURN = false;
	private boolean turn = PLAYERS_TURN;
	
	public void readLvlFromFile(File lvlFile) throws IOException, InvalidLevelFileException {
		StringBuffer lvlString = new StringBuffer();
		
		// Place Reader on Levelfile
		FileReader lvlReader = new FileReader(lvlFile);
		
		// Read Levelfile char by char
		int c;
		while ((c = lvlReader.read()) != -1) {
			lvlString.append((char)c);
		}
		
		readLvlFromString(lvlString.toString());
		
	}
	
	/**
	 * import level from file into program
	 * 
	 * @param file 
	 * @throws IOException
	 * @throws InvalidLevelFileException
	 */
	public void readLvlFromString(String lvl) throws InvalidLevelFileException {
		// Delete old maps
		humanMap = new ExtendedField[10][10];
		cpuMap = new ExtendedField[10][10];

		// Define valid symbols
		Vector<Character> validSymbols = new Vector<Character>(13);
		validSymbols.add('l');
		validSymbols.add('r');
		validSymbols.add('t');
		validSymbols.add('b');
		validSymbols.add('v');
		validSymbols.add('h');
		validSymbols.add('L');
		validSymbols.add('R');
		validSymbols.add('T');
		validSymbols.add('B');
		validSymbols.add('V');
		validSymbols.add('H');
		validSymbols.add('-');
		validSymbols.add('*');
		validSymbols.add('|');
		validSymbols.add('\n');

		// Read Levelfile char by char
		int rc = 0, cc = 0, b = 0; // current Symbol, row counter, column
										// counter, boolean switch between human
										// and cpu map.
		for(char sym: lvl.toCharArray()) {
			// Throw InvalidLevelFileException if file is not formatted correct.
			if (validSymbols.contains(sym) && rc < 10 && cc < 20)
				switch (sym) {
				case '|':
					b++;
					cc = 0;
					break;
				case '\n':
					cc = b = 0;
					rc++;
					break;
				default:
					if(b==0) humanMap[cc][rc] = new ExtendedField(sym);
					if(b==1) cpuMap[cc][rc] = new ExtendedField(sym);
					cc++;
				}
			else {
				throw new InvalidLevelFileException("");
			}
		}
		
	}
	
	public ExtendedField[][] getHumanMap() {
		return humanMap;
	}

	public ExtendedField[][] getCpuMap() {
		return cpuMap;
	}

	/**
	 * Generates string view of the map
	 * 
	 */

	public String toString() {
		if(humanMap == null || cpuMap == null ) return "";
		
		StringBuffer sb = new StringBuffer();
		
		for(int r=0;r<10;r++) {
			for(int c=0;c<10;c++) {
				sb.append(humanMap[c][r].getSymbol());
			}
			sb.append('|');
			for(int c=0;c<10;c++) {
				sb.append(cpuMap[c][r].getSymbol());
			}
			sb.append('\n');
		}
		return sb.toString();
	}

	public void setTurn(boolean turn) {
		this.turn = turn;
	}

	public boolean getTurn() {
		return turn;
	}
}
